package com.hzpd.hainan.module.usercenter.acticity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.model.news.NewsBean;
import com.hzpd.hainan.model.usercenter.CollectBean;
import com.hzpd.hainan.model.usercenter.CollectEntity;
import com.hzpd.hainan.model.usercenter.ComBean;
import com.hzpd.hainan.model.usercenter.ComEntity;
import com.hzpd.hainan.model.video.VideoItemBean;
import com.hzpd.hainan.module.news.activity.NewsHtmlDetailActivity;
import com.hzpd.hainan.module.news.activity.NewsZhuanTiActivity;
import com.hzpd.hainan.module.news.adapter.CommentAdapter;
import com.hzpd.hainan.module.usercenter.adapter.CommAdapter;
import com.hzpd.hainan.module.video.activity.VideoDetailActivity;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.AAnim;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.ParamUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class CommActivity extends ToolBarActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ptrlistview_activity_comm)
    PullToRefreshListView ptrlistview_activity_comm;

    @BindView(R.id.comment_list_tv)
    TextView comment_list_tv;

    CommAdapter adapter;
    private int Page = 1;
    private static final int PageSize = 15;
    private boolean mFlagRefresh = true;//刷新还是加载
    private List<ComBean> list;

    @Override
    public void initView() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            tv_title.setText("我的评论");
            comment_list_tv.setText("评论列表");
        }else{
            tv_title.setText("དཔྱད་བརྗོད།");
            comment_list_tv.setText("རེའུ་མིག");
        }
    }

    @Override
    public String initToolBarTitle() {
        return null;
    }

    @Override
    public int setMyContentView() {
        return R.layout.activity_comm;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = new ArrayList<>();
        adapter = new CommAdapter(activity);
        ptrlistview_activity_comm.setAdapter(adapter);
        ptrlistview_activity_comm.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mycommItemclick(adapterView, view, i, l);
            }
        });
        ptrlistview_activity_comm.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                ptrlistview_activity_comm.getLoadingLayoutProxy().setPullLabel("下拉刷新");
                Page = 1;
                mFlagRefresh = true;
                getCommForServer();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                ptrlistview_activity_comm.getLoadingLayoutProxy().setPullLabel("");
                Page++;
                mFlagRefresh = false;
                getCommForServer();
            }
        });
        ptrlistview_activity_comm.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptrlistview_activity_comm.setRefreshing(true);
            }
        }, 500);
    }

    private void mycommItemclick(AdapterView<?> adapterView, View view, int i, long l) {
        ComBean cb = (ComBean) adapter.getItem(i - 1);
        Intent intent = new Intent();
        intent.putExtra("from", "comm");
        boolean flag = false;//是否是预定类型
        //跳转脚标）1新闻  2图集  3视频 4html5
        LogUtils.e("type-->" + cb.getInfo().getRtype());


        if ("1".equals(cb.getInfo().getContenttype())) {
            NewsBean nb = new NewsBean();
            nb.setNid(cb.getInfo().getContentid());
            nb.setImgs(cb.getInfo().getImgs());
            intent.putExtra("newbean", nb);
            flag = true;
            intent.setClass(this, NewsHtmlDetailActivity.class);
        } else if ("3".equals(cb.getInfo().getContenttype())) {
            VideoItemBean nb = new VideoItemBean();
            nb.setVid(cb.getInfo().getContentid());
            nb.setTitle(cb.getInfo().getTitle());
            nb.setVideourl(cb.getInfo().getVideourl());
            intent.putExtra("videoItemBean", nb);
            flag = true;
            intent.setClass(this, VideoDetailActivity.class);
        } else if ("4".equals(cb.getInfo().getContenttype())) {
            NewsBean nb = new NewsBean();
            nb.setNid(cb.getInfo().getContentid());
            nb.setImgs(cb.getInfo().getImgs());
            nb.setComflag("1");
            intent.putExtra("newbean", nb);
            flag = true;
            intent.setClass(this, NewsZhuanTiActivity.class);
        }
        if (flag) {
            startActivity(intent);
            AAnim.startScreen(activity);
        }
    }

    private void getCommForServer() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", spu.getUser().getUid());
        map.put("siteid", "1");
        Factory.provideHttpService().my_comm(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<ComEntity, Boolean>() {
                    @Override
                    public Boolean call(ComEntity comentity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ComEntity>() {
                    @Override
                    public void call(ComEntity comentity) {
                        LogUtils.e("code--" + comentity.code);
                        ptrlistview_activity_comm.onRefreshComplete();
                        if ("200".equals(comentity.code)) {
                            LogUtils.e("code--" + comentity.msg);


                            addDataList(comentity.data);
                            LogUtils.e(list.size() + "");
                        } else if ("209".equals(comentity.code)) {
                            adapter.clear();
                            adapter.removeOld();
                            list.clear();
                            adapter.notifyDataSetChanged();
                        } else {
                            if (!mFlagRefresh) {
                                Page--;
                            }
                        }
                        mFlagRefresh = false;
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable--" + throwable.getMessage());
                    }
                });
    }

    private void addDataList(List<ComBean> mlist) {
        adapter.appendData(mlist, true);
        LogUtils.e(list.size() + "");
        adapter.notifyDataSetChanged();
        ptrlistview_activity_comm.setAdapter(adapter);
    }
}

